package logic

import (
	"context"
	"golang.org/x/tools/godoc/redirect"
	"net/http"
	"week1_zg6/orderrpc/order"

	"week1_zg6/orderapi/internal/svc"
	"week1_zg6/orderapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PayLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPayLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PayLogic {
	return &PayLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PayLogic) Pay(req *types.PayRequest) (resp *types.Response, err error) {
	pays, err := l.svcCtx.Order.Pay(context.Background(), &order.PayRequest{
		OrderID: req.OrderID,
		Amount:  req.Amount,
	})
	if err != nil {
		return nil, err
	}
	redirect.Handler(pays.Data)
	return &types.Response{
		Code: http.StatusOK,
		Msg:  pays.Data,
		Date: nil,
	}, nil
}
